home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / boss.arc / HELP.C < prev    next >
Text File  |  1987-06-04  |  9KB  |  255 lines

  1. /*
  2. ** HELP - Help driver for The Window Boss
  3. **
  4. ** Copyright (c) 1985 - Philip A. Mongelluzzo
  5. ** All rights reserved.
  6. **
  7. ** Comments:
  8. **
  9. **  Help files must be formatted properly, use GENINDEX to create the index
  10. **  file.
  11. **
  12. */
  13.  
  14. /* Header files */
  15.  
  16. #include "windows.h"
  17. #include "math.h"                       /* needed for atol */
  18.  
  19. /* Equates & definitions */
  20.  
  21. #define MAXKEY 255                      /* ISAM file length (# of keys) */
  22. #define FALSE 0                         /* lies */
  23. #define TRUE ~FALSE                     /* Science */
  24. #define RD "rb"                         /* file access mode */
  25.  
  26. #define K_PGUP 0x49                     /* a few scan codes */
  27. #define ESC    0x01
  28.  
  29. #if CI86                                /* Compiler specific stuff */
  30. #define atol atoi                       
  31. #endif
  32.  
  33. /* Function declarations */
  34.  
  35. extern FILE *fopen();                   /* Stream I/O */
  36. #ifndef BORLAND
  37. extern char *malloc();                  /* memory mgmt */
  38. #endif
  39. extern char *fgets();                   /* file line input */
  40. extern char *strcpy();                  /* string copy */
  41. extern char *strcat();                  /* concat function */
  42. extern long atol();                     /* ascii to integer */
  43. extern int fseek();                     /* DASD seek */
  44. extern long ftell();                    /* where are we DASD */
  45.  
  46. static struct list {                    /* master list structure */
  47.   char *key;                            /* pointer to %info string */
  48.   long loc;                             /* file offset */
  49.   int type;                             /* type <0=toc; 1=page; 2=menu> */
  50. } master[MAXKEY];                       /* master ISAM structure */
  51.  
  52. static char *scrbuf[26];                /* screen buffer (array of ptrs) */
  53. static char line[80];                   /* line buffer */
  54. static int cpg, eoh;                    /* clear screen, end of help flags */
  55. static FILE *fp;                        /* stream file pointer */
  56.  
  57. static WINDOWPTR wn1;                   /* window pointer */
  58. static int row, col;                    /* original cursor location */
  59.  
  60. /*
  61. *************
  62. * help_init *
  63. *************
  64. */
  65.  
  66. help_init(help_file_name)               /* init help function */
  67. char *help_file_name;
  68. {
  69.   if(!help("@"))                        /* allocate the memory */
  70.     return(FALSE);                      /* return if error */
  71.   return(help(help_file_name));         /* setup help ISAM */
  72. }
  73.  
  74. /*
  75. ********
  76. * help *
  77. ********
  78. */
  79.  
  80. help(subject)                           /* help function */
  81. char *subject;                          /* help subject request */
  82. {
  83. char tmpname[30];                       /* temporary filename */
  84. char *p;                                /* scratch pointer */
  85. int i,j,k,l,m,n;                        /* scratch integers */
  86. int showpage();                         /* display page function */
  87. int showmenu();                         /* display menu function */
  88. static initflg = FALSE;                 /* init complete flag */
  89.  
  90.   switch(subject[0]) {                  /* on first char of subject ... */
  91.     case '%':                           /* subject page */
  92.       wn1 = wn_open(0, 0, 0, 78, 23, BLUE<<4 | WHITE, BLUE<<4 | WHITE);
  93.       if(!wn1) return(FALSE);           /* error return */
  94.       wn_wrap(wn1,FALSE);               /* no wrap thank you */
  95.       v_rcpos(0, &row, &col);           /* remember current location */
  96.       v_hidec();                        /* hide the physical cursor */
  97.       wn_locate(wn1,0,0);               /* home virtual cursor */
  98.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  99.       i=0;                              /* start search index */
  100.       while(i < MAXKEY) {               /* look for match in key table */
  101.         if(!strcmp(subject,master[i].key)) {
  102.           showpage(master[i].loc);      /* display all pages */
  103.           return(TRUE);                 /* return .. all is well */
  104.         }
  105.         i++;
  106.       }
  107.       wn_printf(wn1," Sorry... No info on %s\n", subject);
  108.       wn_printf(wn1," Press any key to continue...");
  109.       v_getch();
  110.       wn_close(wn1);
  111.       return(FALSE);                    /* no match !! */
  112.       break;
  113.     case '$':                           /* menu page */
  114.       break;                            /* sometime in the future */
  115.     case '@':                           /* Initialize (allocate memory) */
  116.       for(i=0; i<MAXKEY; i++) {
  117.         master[i].key = malloc(25);     /* keys can be 25 chars long */
  118.         if(!master[i].key) return(FALSE);
  119.       }
  120.       for(i=0; i<25; i++) {             /* allocate memory for screen */
  121.         scrbuf[i] =  malloc(81);        /* buffer */
  122.         if(!scrbuf[i]) return(FALSE);
  123.       }
  124.       initflg = TRUE;
  125.       return(TRUE);
  126.       break;
  127.     default:                            /* assume help filename */
  128.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  129.       strcpy(tmpname,subject);          /* form filename from root */
  130.       strcat(tmpname,".ndx");
  131.       if(!(fp = fopen(tmpname,RD))) {   /* index file cant be found ! */
  132.         printf("Can't find index file\n");
  133.         printf("Press any key to continue...");
  134.         v_getch();
  135.         return(FALSE);
  136.       }
  137.       i = 0;                            /* read and process key file */
  138.       while(fgets(line,80,fp)) {        /* build the master index table */
  139.         strcpy(master[i].key, line);
  140.         if(line[0] != '%')              /* ignore all but subects */
  141.           continue;
  142.         fgets(line,80,fp);
  143.         master[i].loc = atol(line);
  144.         i++;
  145.       }
  146.       fclose(fp);                       /* close index file */
  147.       strcpy(tmpname, subject);         /* form help file name from root */
  148.       strcat(tmpname, ".hlp");          /* open and leave open ! */
  149.       if(!(fp = fopen(tmpname, RD))) return(FALSE);
  150.       return(TRUE);
  151.       break;
  152.   }
  153. }
  154.  
  155. /*
  156. ************
  157. * showpage *
  158. ************
  159. */
  160.  
  161. static showpage(offset)                 /* display page function */
  162. long offset;
  163. {
  164. int i,j;                                /* scratch integers */
  165. long pages[MAXKEY];                     /* stack of pages */
  166. int pndx;                               /* stack pointer */
  167. int keyflg;                             /* vaild key flag */
  168.  
  169.   pndx = 0;                             /* init stack pointer */
  170.   pages[0] = offset;                    /* initial page */
  171.  
  172.   while(TRUE) {                         /* till forever ... */
  173.     i = filbuf(offset);                 /* fill the buffer */
  174.     for(j=0; j<i; j++)                  /* display buffer */
  175.       wn_puts(wn1,j,0,scrbuf[j]);
  176.     wn1->style |= BOLD;                 /* toggle bold on */
  177.     if(eoh) 
  178.       wn_puts(wn1, 22, 0, " End of help, PgUp for previous screen, any other key to continue...");
  179.     else
  180.       wn_puts(wn1, 22, 0, " Esc to quit help, PgUp for previous screen, any other key to continue...");
  181.     wn1->style ^= BOLD;                 /* toggle bold off */
  182.     keyflg = FALSE;                     /* valid key flag */
  183.     do {                                /* loop till vaild key */
  184.       switch (v_getch() >> 8) {         /* based on user input */
  185.         case K_PGUP:                    /* do all the right things */
  186.           pndx--;
  187.           if(pndx <0) {
  188.             wn_close(wn1);
  189.             v_locate(0, row, col);
  190.             return;
  191.           }
  192.           offset = pages[pndx];
  193.           keyflg = TRUE;
  194.           eoh = FALSE;                  /* force clear screen */
  195.           cpg = TRUE;                   /* and cancel end of help flag */
  196.           break;
  197.         case ESC:
  198.           wn_close(wn1);
  199.           v_locate(0, row, col);
  200.           return;
  201.         default:
  202.           offset = pages[++pndx] = ftell(fp);
  203.           keyflg = TRUE;
  204.           break;
  205.       }
  206.     } while (!keyflg);                  /* loop till valid key */
  207.     if(cpg) wn_clr(wn1);
  208.     if(eoh) {
  209.       wn_close(wn1);
  210.       v_locate(0, row, col);
  211.       return;
  212.     }
  213.   }
  214. }
  215.  
  216. /*
  217. **********
  218. * filbuf *
  219. **********
  220. */
  221. static filbuf(offset)                   /* fill screen buffer */
  222. long offset;
  223. {
  224. int i,j;
  225. char *p, *p1;
  226.  
  227.   i=0;                                  /* initialize */
  228.   cpg = eoh = FALSE;                    /* assume nothing */
  229.   fseek(fp, offset, 0);                 /* position file */
  230.   do {
  231.     fgets(line, 80, fp);                /* fetch a line */
  232.       cpg = !strcmp(".cp\r\n", line);   /* lite clear screen and */
  233.       eoh = !strcmp("*END*\r\n",line);  /* end of help flags */
  234.       if(cpg || eoh) break;
  235.       p = &line[0];                     /* a long way around to */
  236.       p1 = scrbuf[i];                   /* filter the carriage */
  237.       while (*p) {                      /* returns !! */
  238.         switch (*p) {
  239.           case '\r':
  240.           case '\n':
  241.             p++;
  242.             break;
  243.           default:
  244.             *p1++ = *p++;
  245.             break;
  246.         }
  247.       }
  248.       *p1 = NULL;
  249.       i++;
  250.   } while (TRUE);
  251.   return(i);                            /* return the # of lines on page */
  252. }
  253.  
  254. /* End */
  255.